var graduates = []
var tbodys = document.querySelector("tbody")
var pageSize = 6; //每页显示的记录条数
var page = 1; //当前页，默认第一页

// 查询在习学生档案
function selectstu() {
    let stu = {
        pageIndex: page,
        pageSize: pageSize,
        fileType: 1
    }
    ajaxHttp("post", "file/fileList", stu).then((res) => {
        // console.log(res);
        res = JSON.parse(res)
        var data = res.dataset.fileList
        console.log(data);
        graduate = data
        for (var i = 0; i < graduate.length; i++) {
            var newtr = document.createElement("tr")
            newtr.innerHTML = `
             <td>${graduate[i].fileName}</td>
                            <td>${graduate[i].fileSex}</td>
                            <td class="filecla">${graduate[i].fileClazz}</td>
                            <td>${graduate[i].fileDate}</td>
                            <td>${graduate[i].filePhone}</td>
                            <td class="file">${graduate[i].fileStatus}</td>
                            <td>${graduate[i].fileAdress}</td>
                            <td>${graduate[i].sosPhone}</td>
                            <td>${graduate[i].sosPerson}</td>
                            <td class="operate">
                            <button class="revise" onclick="revise(${graduate[i].fileId})" style="color:#1BB27F; cursor:pointer;">修改</button>
                            <button onclick="deltr(${graduate[i].fileId},${graduate[i].userAccount})" style="color:#E86A73;cursor:pointer;">删除</button>
                        </td>
        `
            tbodys.appendChild(newtr)

        }
        file()
        claid()
    })
}
selectstu()


// 按照名字查询
function selename() {
    var selname = document.querySelector("#selname").value
    // console.log(selname);
    let sele = {
        pageIndex: page,
        pageSize: pageSize,
        fileName: selname,
        fileType: 1
    }
    ajaxHttp("post", "file/fileList", sele).then((res) => {
        // console.log(res);
        res = JSON.parse(res)
        var data = res.dataset.fileList
        console.log(selname);
        graduates = data
        console.log(graduates);
        for (var i = 0; i < graduates.length; i++) {
            if (selname == graduates[i].fileName) {
                
                tbodys.innerHTML = ""
               for(var j =0;j<graduates.length;j++){
                var newtr = document.createElement("tr")
                newtr.innerHTML = `
                <td>${graduates[j].fileName}</td>
                <td>${graduates[j].fileSex}</td>
                <td class="filecla">${graduates[j].fileClazz}</td>
                <td>${graduates[j].fileDate}</td>
                <td>${graduates[j].filePhone}</td>
                <td class="file">${graduates[j].fileStatus}</td>
                <td>${graduates[j].fileAdress}</td>
                <td>${graduates[j].sosPhone}</td>
                <td>${graduates[j].sosPerson}</td>
                <td class="operate">
                <button class="revise" onclick="revise();account()" style="color:#1BB27F; cursor:pointer;">修改</button>
                <button onclick="deltr()" style="color:#E86A73;cursor:pointer;">删除</button>
            </td>
                `
                tbodys.appendChild(newtr)
               }
            }

        }
        console.log(res);
        file()
        claid()
    })
}


//判断班级序号
function claid() {
    var filecla = document.querySelectorAll(".filecla")
    for (var i = 0; i < filecla.length; i++) {
        if (filecla[i].innerText === "1") {
            filecla[i].innerText = "UI设计班"
        } else if (filecla[i].innerText === "2") {
            filecla[i].innerText = "web前端班"
        } else if (filecla[i].innerText === "3") {
            filecla[i].innerText = "Java软件开发班"
        } else if (filecla[i].innerText === "4") {
            filecla[i].innerText = "前端全栈班"
        } else if (filecla[i].innerText === "5") {
            filecla[i].innerText = "java"
        }
    }
}

// 判断学生状态
function file() {
    var file = document.querySelectorAll(".file")
    for (var i = 0; i < file.length; i++) {
        // console.log(file[i]);
        if (file[i].innerText === "0") {
            file[i].innerText = "未毕业"
            file[i].style.color = "green"
        } else if (file[i].innerText === "1") {
            file[i].innerText = "已毕业"
            file[i].style.color = "red"
        }
    }
}

var opens = document.querySelector(".open")
// 单击修改打开修改弹窗
function revise(rev) {
    var open=document.querySelector(".open")
    console.log(open);
    var names=open.querySelector(".names")
    var sex=open.querySelector(".sex")
    var tel=open.querySelector(".tel")
    var souser=open.querySelector(".souser")
    var clazz=open.querySelector(".clazz")
    var datetime=open.querySelector("#datetime")
    var adress=open.querySelector(".adress")
    var sophone=open.querySelector("#sophone")
    var pwd=open.querySelector(".pwd")
    var acc=open.querySelector("#account")
    opens.style.display="flex"
    // 在打开遮罩的时候获取到当前行的tr数据
    localStorage.setItem("fileid",rev)
    // console.log(this)
    var obs=this.graduate
    
    // console.log(obs);
    for(var i=0;i<obs.length;i++){
        // console.log(obs[i]);
        if(rev===obs[i].fileId){
            names.value=obs[i].fileName
            sex.value=obs[i].fileSex
            tel.value=obs[i].filePhone
            souser.value=obs[i].sosPerson
            clazz.value=obs[i].fileClazz
            datetime.value=obs[i].fileDate
            adress.value=obs[i].fileAdress
            sophone.value=obs[i].sosPhone
            pwd.value=obs[i].password
            acc.value=obs[i].userAccount
        }
    }
}
function revuser(){
    var open=document.querySelector(".open")
    var names=open.querySelector(".names")
    var sex=open.querySelector(".sex")
    var tel=open.querySelector(".tel")
    var souser=open.querySelector(".souser")
    var clazz=open.querySelector(".clazz")
    var datetime=open.querySelector("#datetime")
    var adress=open.querySelector(".adress")
    var sophone=open.querySelector("#sophone")
    var pwd=open.querySelector(".pwd")
    var acc=open.querySelector("#account")
    var ind=localStorage.getItem("fileid")
    var num=Number(ind)
    let updata={
        fileId:num,
        fileName:names.value,
        fileClazz:clazz.value,
        fileSex:sex.value,
        filePhone:tel.value,
        fileAdress:adress.value,
        SosPerson:souser.value,
        password:pwd.value,
        fileDate:datetime.value,
        SosPhone:sophone.value,
        userAccount:acc.value,
        fileType:1
    }
    ajaxHttp("post","file/updateFile",updata).then((res)=>{
    //    console.log(res);
        alert("修改成功")
        offrev()
        location.reload()
    })
    
}
// 单击修改取消关闭遮罩
function offrev() {
    opens.style.display = "none"
}
// 删除功能

// 打开弹窗
var del=document.querySelector(".del")
function deltr(ters,userac) {
    // 调用删除接口
    del.style.display="flex"
    localStorage.setItem("fileid",ters)
    localStorage.setItem("useraccount",userac)
}
// 调用删除接口
function delstud(){
    var ind=localStorage.getItem("fileid")
    var num=Number(ind)
    var user=localStorage.getItem("useraccount")
    let delstu={
        fileId:num,
        userAccount:user
    }
    ajaxHttp("post","file/deleteFile",delstu).then((res)=>{
        alert("删除成功")
        offdel()
        location.reload()
    })
    
}
// 关闭弹窗
function offdel(){
    del.style.display="none"
}

// 打开添加用户
var addopens = document.querySelector(".addopen")
function addopen() {
    addopens.style.display = "flex"
    // 账号验重
    var acc=addopens.querySelector("#account")
    let accounts={
        Account:acc.value
    }
    ajaxHttp("post","file/chickAccount",accounts).then((res)=>{
        // console.log(res);
        if(res.code==200){
            console.log(账号无重复);
        }else if(res.code==500){
            console.log(账号有重复);
        }
    })
    account()
}
function adduser(){
    var addopen=document.querySelector(".addopen")
    var names=addopen.querySelector(".names")
    var sex=addopen.querySelector(".sex")
    var tel=addopen.querySelector(".tel")
    var souser=addopen.querySelector(".souser")
    var clazz=addopen.querySelector(".clazz")
    var datetime=addopen.querySelector("#datetime")
    var adress=addopen.querySelector(".adress")
    var sophone=addopen.querySelector("#sophone")
    var pwd=addopen.querySelector(".pwd")
    var acc=addopen.querySelector("#account")
    let stude={
        fileName:names.value,
        fileSex:sex.value,
        fileClazz:clazz.value,
        fileDate:datetime.value,
        filePhone:tel.value,
        fileAdress:adress.value,
        SosPerson:souser.value,
        SosPhone:sophone.value,
        Account:acc.value,
        fileType:1,
        password:pwd.value
    }
    ajaxHttp("post","file/addfile",stude).then((res)=>{
        alert("添加成功")
        offadd()
        location.reload()
    })
}
function offadd() {
    addopens.style.display = "none"
}

// 随机生成账号
var accounts = document.querySelector("#account")
function account() {
    var num = ""//存放用户账号
    for (var i = 0; i < 2; i++) {
        num += Math.floor(Math.random() * 10)
    }
    var time = new Date()
    var y = time.getFullYear()
    var m = time.getMonth() + 1
    var data = time.getDate()
    // num=y+num
    num = "BR" + y.toString() + m.toString() + data.toString() + num
    accounts.value = num
    return num
}

// 分页
var theUL = document.getElementById("pageListTableBody");
var totalPage = document.getElementById("spanTotalPage");
var pageNum = document.getElementById("spanPageNum"); //获取当前页<span>
var spanPre = document.getElementById("spanPre"); //获取上一页<span>
var spanNext = document.getElementById("spanNext"); //获取下一页<span>
var spanFirst = document.getElementById("spanFirst"); //获取第一页<span>
var spanLast = document.getElementById("spanLast"); //获取最后一页<span>
var numberRowsInTable = theUL.getElementsByTagName("tr").length; //记录总条数


//下一页
function next() {
    hideTable();
    currentRow = pageSize * page;
    maxRow = currentRow + pageSize;
    if (maxRow > numberRowsInTable) maxRow = numberRowsInTable;
    for (var i = currentRow; i < maxRow; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    page++;
    if (maxRow == numberRowsInTable) {
        nextText();
        lastText();
    }
    showPage();
    preLink();
    firstLink();
}

//上一页
function pre() {
    hideTable();
    page--;
    currentRow = pageSize * page;
    maxRow = currentRow - pageSize;
    if (currentRow > numberRowsInTable) currentRow = numberRowsInTable;
    for (var i = maxRow; i < currentRow; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    if (maxRow == 0) {
        preText();
        firstText();
    }
    showPage();
    nextLink();
    lastLink();
}

//第一页
function first() {
    hideTable();
    page = 1;
    for (var i = 0; i < pageSize; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    firstText();
    preText();
    nextLink();
    lastLink();
}

//最后一页
function last() {
    hideTable();
    page = pageCount();
    currentRow = pageSize * (page - 1);
    for (var i = currentRow; i < numberRowsInTable; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    preLink();
    nextText();
    firstLink();
    lastText();
}

function hideTable() {
    for (var i = 0; i < numberRowsInTable; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
}

function showPage() {
    pageNum.innerHTML = page;
}

//总共页数
function pageCount() {
    return Math.ceil(numberRowsInTable / pageSize);
}
//显示链接
function preLink() {
    spanPre.innerHTML = "<a href='javascript:pre();'>上一页</a>";
}
function preText() {
    spanPre.innerHTML = "上一页";
}
function nextLink() {
    spanNext.innerHTML = "<a href='javascript:next();'>下一页</a>";
}
function nextText() {
    spanNext.innerHTML = "下一页";
}
function firstLink() {
    spanFirst.innerHTML = "<a href='javascript:first();'>首页</a>";
}
function firstText() {
    spanFirst.innerHTML = "首页";
}
function lastLink() {
    spanLast.innerHTML = "<a href='javascript:last();'>末页</a>";
}
function lastText() {
    spanLast.innerHTML = "末页";
}
//隐藏
function hide() {
    for (var i = pageSize; i < numberRowsInTable; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
    totalPage.innerHTML = pageCount();
    pageNum.innerHTML = '1';
    nextLink();
    lastLink();
}
hide();